CONTENTS | INDEX | PREV | NEXT
 ftell

 NAME
  ftell - return current position within file pointer

 SYNOPSIS
  #include <stdio.h>

  long pos = ftell(fp);
  FILE *fp;

 FUNCTION
  ftell() returns the current absolute seek offset within a file
  pointer.

 EXAMPLE
  /*
   *  get a line, save current position, get rest of file, go back
   *  to saved position, retrieve line again and print again.
   *
   *  like fsetpos() example but uses ftell()/fseek() instead
   */

  #include <stdio.h>

  main(ac, av)
  int ac;
  char **av;
  {
      FILE *fp;
      long save_pos;
      int count;
      char buf[256];

      if (ac == 1) {
      puts("Expected textfile argument");
      exit(1);
      }
      fp = fopen(av[1], "r");
      if (fp == NULL) {
      printf("Unable to open %sn", av[1]);
      exit(1);
      }
      for (count = 0; fgets(buf, sizeof(buf), fp); ++count) {
      if (count == 0)             /*  just before second line */
          save_pos = ftell(fp);
      fprintf(stdout, "%-3d: %s", count + 1, buf);
      }
      if (count < 2) {
      puts("not enough lines in file for example!");
      exit(1);
      }
      puts("--end of file, now seeking back to line 2--");
      fseek(fp, save_pos, SEEK_SET);
      if (fgets(buf, sizeof(buf), fp) == NULL) {
      puts("error!");
      exit(1);
      }
      fprintf(stdout, "%-3d: %s", 2, buf);
      fclose(fp);
      return(0);
  }

 INPUTS
  FILE *fp;       file pointer retrieve seek position from


 RESULTS
  long pos;       current absolute seek position in file

 SEE ALSO
  ftell, fgetpos, fsetpos, fseek, rewind